﻿using Core.Framework.Model.Common;
using Core.Framework.Redis;
using Core.Framework.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.IBusiness.IComment.Model
{
    /// <summary>
    /// 评论redis缓存工具箱
    /// </summary>
    public class CommentRedisCache
    {
        /// <summary>
        /// 评论 TableName
        /// </summary>
        public readonly string commentStarTableName = "comment_star";

        /// <summary>
        /// 评论回复TableName
        /// </summary>
        public readonly string commentReplyStarTableName= "comment_reply_star";

        /// <summary>
        /// redis 仓库
        /// </summary>
        public readonly int redisDB = CoreStartupHelper.GetConfigValue("Redis:Comment").ToInt(1);


        /// <summary>
        /// 标识列
        /// </summary>
        private int key { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key">标识列</param>
        public CommentRedisCache(int key)
        {
            this.key = key;
        }


        #region 评论

        #region 赞

        /// <summary>
        /// 点赞评论
        /// </summary>
        public void CommentStar(int userKey)
        {
            var redis = new RedisHelper(redisDB);
            redis.HashSet(this.HashTableName(this.commentStarTableName), userKey.ToString(), 1);
        }

        /// <summary>
        /// 取消点赞评论
        /// </summary>
        public void CommentUnStar(int userKey)
        {
            var redis = new RedisHelper(redisDB);
            redis.HashDelete(this.HashTableName(this.commentStarTableName), userKey.ToString());
        }

        /// <summary>
        /// 获取点赞评论
        /// </summary>
        public List<KeyValuePair<string, string>> GetCommentStar()
        {
            var redis = new RedisHelper(redisDB);
            return redis.HashGetAll(this.HashTableName(this.commentStarTableName));
        }

        #endregion


        #endregion


        #region 评论回复

        #region 赞

        /// <summary>
        /// 点赞评论回复
        /// </summary>
        public void CommentReplyStar(int userKey)
        {
            var redis = new RedisHelper(redisDB);
            redis.HashSet(this.HashTableName(this.commentReplyStarTableName), userKey.ToString(), 1);
        }

        /// <summary>
        /// 取消评论回复点赞
        /// </summary>
        public void CommentReplyUnStar(int userKey)
        {
            var redis = new RedisHelper(redisDB);
            redis.HashDelete(this.HashTableName(this.commentReplyStarTableName), userKey.ToString());
        }

        /// <summary>
        /// 获取评论回复点赞
        /// </summary>
        public List<KeyValuePair<string, string>> GetCommentReplyStar()
        {
            var redis = new RedisHelper(redisDB);
            return redis.HashGetAll(this.HashTableName(this.commentReplyStarTableName)) ?? new List<KeyValuePair<string, string>>();
        }

        #endregion


        #endregion



        /// <summary>
        /// 获取HashName
        /// </summary>
        /// <returns></returns>
        private string HashTableName(string table)
        {
            return string.Format("{0}_{1}", table, this.key);
        }

    }
}
